package com.share.tool.generator.api.postman;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.io.Files;
import com.share.common.constant.DirConstant;
import com.share.common.enums.MethodMenu;
import com.share.common.utils.DateUtil;
import com.share.tool.generator.api.config.BaseInfo;
import com.share.tool.generator.api.model.CtrlInfo;
import com.share.tool.generator.api.model.MethodInfo;
import com.share.tool.generator.api.model.ParamInfo;
import com.share.tool.generator.api.postman.model.*;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/11/20 13:08
 */
public class PostManFormat {
    public static String convertAndToFile(BaseInfo base, List<CtrlInfo> list) {
        String converData = convert(base, list);
        if (StringUtils.isEmpty(converData)) {
            return null;
        }

        File file = new File(DirConstant.TMP_DIR + File.separator + "postMan.json");
        try {
            Files.write(converData.getBytes(), file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return file.getAbsolutePath();
    }

    public static String convert(BaseInfo base, List<CtrlInfo> list) {
        if (list == null) {
            return null;
        }

        JsonRoot jr = new JsonRoot();
        String batchNum = DateUtil.getDateTime();
        jr.setInfo(new Info(batchNum, base.getContext() + "-" + batchNum));
        List<ApiCtrl> ctrlList = new LinkedList<>();
        jr.setItem(ctrlList);
        for (CtrlInfo ctrl : list) {
            ApiCtrl apiCtrl = new ApiCtrl();
            apiCtrl.setName(ctrl.getDescrip());
            ctrlList.add(apiCtrl);

            List<ApiMethod> methodList = new LinkedList<>();
            apiCtrl.setItem(methodList);
            List<MethodInfo> methods = ctrl.getMethodList();
            for (MethodInfo method : methods) {
                ApiMethod apiMethod = new ApiMethod();
                methodList.add(apiMethod);
                apiMethod.setName(method.getDescrip());

                ApiRequest apiRequest = new ApiRequest();
                apiMethod.setRequest(apiRequest);
                if (method.getResponse() != null && !method.getResponse().isEmpty()) {/*返回的数据结构*/
                    apiMethod.setResponse(method.getResponse());
                }
                apiRequest.setMethod(method.getMethod());

                ApiUrl apiUrl = new ApiUrl();
                apiRequest.setUrl(apiUrl);

                if (base.getHeaders() != null) {
                    String strJson = JSON.toJSONString(base.getHeaders());
                    List<ApiHeader> apiHeaderList = JSON.parseArray(strJson, ApiHeader.class);
                    apiRequest.setHeader(apiHeaderList);
                }

                List<String> reqUrl = new LinkedList<>();
                if (!StringUtils.isEmpty(base.getContext())) {
                    reqUrl.add(base.getContext());
                }
                if (ctrl.getMapping() != null) {
                    reqUrl.addAll(Arrays.asList(ctrl.getMapping()));
                }
                if (method.getMapping() != null) {
                    reqUrl.addAll(Arrays.asList(method.getMapping()));
                }
                apiUrl.setPath(reqUrl.toArray(new String[reqUrl.size()]));
                String[] hosts = {base.getHost()};
                apiUrl.setHost(hosts);
                apiUrl.setPort(base.getPort());
                apiUrl.setProtocol(base.getProtocol());

                List<ParamInfo> params = method.getParamList();
                if (params != null && !params.isEmpty()) {
                    if (MethodMenu.GET.code.equals(method.getMethod())) {
                        List<ApiParam> paramList = new LinkedList<>();
                        apiUrl.setQuery(paramList);
                        for (ParamInfo param : params) {
                            ApiParam apiParam = new ApiParam();
                            apiParam.setDescription("(" + param.getType() + ") " + param.getDescrip());
                            apiParam.setKey(param.getKey());
                            //apiParam.setValue(null);
                            paramList.add(apiParam);
                        }
                    } else if (MethodMenu.POST.code.equals(method.getMethod())) {
                        ApiBody apiBody = new ApiBody();
                        JSONObject rowJsonObj = new JSONObject();
                        apiRequest.setBody(apiBody);
                        for (ParamInfo param : params) {
                            String key = param.getKey();
                            if ("serialVersionUID".equals(key)) {
                                continue;
                            }
                            String descript = ("(" + param.getType() + ") " + param.getDescrip());
                            rowJsonObj.put(key, "");/*设置为空*/
                        }
                        String rawvalue = rowJsonObj.toJSONString();
                        apiBody.setRaw(rawvalue);
                    }
                }
            }
        }
        return JSON.toJSONString(jr, SerializerFeature.WriteMapNullValue);
    }
}



















